package cn.edu.scau.cmi.wuzhenxin.composite.safe;

import javafx.fxml.FXML;

import javafx.scene.control.Button;

import java.util.HashMap;
import java.util.Map;
import javafx.event.ActionEvent;

import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;

import javafx.scene.control.CheckBox;


public class MyController {
	@FXML
	private AnchorPane InsureCom;
	@FXML
	private Button btn_upper;
	@FXML
	private Button btn_confrim;
	@FXML
	private TextArea detilText;
	@FXML
	private CheckBox individual;
	@FXML
	private CheckBox organization;
	@FXML
	private TextField Text_newObject;
	@FXML
	private TextField Text_itsFather;
	private SafeComponent safeComponent = null;
	public Map<String,SafeComponent> safeComponentList = new HashMap<String,SafeComponent>();
	public void eventButtonConfirm(ActionEvent event) {
		if(this.safeComponentList.containsKey(this.Text_newObject.getText())) {
			this.detilText.setText("不能创建重复对象");
			return;
		}
		if(this.organization.isSelected()) {//确认创建的是个人还是组织
			this.safeComponent = new SafeComposite(this.Text_newObject.getText());
		}else if(this.individual.isSelected()) {
			this.safeComponent = new SafeLeaf(this.Text_newObject.getText());
		}
		
		System.out.println("儿子" + this.Text_newObject.getText());
		
		safeComponentList.put(this.Text_newObject.getText(), this.safeComponent);
		
		if(this.Text_itsFather.getText().equals("")) {//父亲不填则是根目录
			this.safeComponent.setParent(null);
		}else {
			this.safeComponent.setParent(safeComponentList.get(this.Text_itsFather.getText()));
			System.out.println("父亲" +this.Text_itsFather.getText());
		}
	}
	
	public void eventButtonShow() {//显示当前对象的上层结构
		SafeComponent.number = 0;
		if(this.safeComponent == null) {
			this.detilText.setText("当前已是根目录");
			return;
		}
			
		this.safeComponent = this.safeComponent.getParent();
		if(this.safeComponent == null)
			this.detilText.setText("当前已是根目录");
		else
			this.detilText.setText(this.safeComponent.say());
		System.out.print("666 ");
	}
	
	public void choiceIndividual(ActionEvent event) {//用于保证是单选框
		if(this.individual.isSelected()) {
			this.organization.setSelected(false);
		}else {
			this.organization.setSelected(true);
		}	
	}
	
	public void choiceOrganization(ActionEvent event) {//用于保证是单选框
		if(this.organization.isSelected()) {
			this.individual.setSelected(false);
		}else {
			this.individual.setSelected(true);
		}
	}
}